//
//  YLMemberViewModel.m
//  YLFinanceService
//
//  Created by she1323 on 2017/9/11.
//  Copyright © 2017年 she1323. All rights reserved.
//

#import "YLMemberViewModel.h"
#import "WHNetworkTool.h"
#import "WHProgressHUD.h"
#import "YLAccountTool.h"
#import <YYModel.h>
#import "YLMemberItemGroup.h"
@implementation YLMemberViewModel


- (instancetype)init
{
    if (self = [super init]) {
        [self setUp];
    }
    return self;
}

- (void)setUp
{
    __weak typeof(self) weakSelf = self;
    _privilegeListCommend = [[RACCommand alloc] initWithSignalBlock:^RACSignal * _Nonnull(id  _Nullable input) {
        return [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
            
            NSDictionary *param = @{@"PrivilegeID" : @(-1)};
            
            NSString *url = [NSString stringWithFormat:@"%@AppCommon/PGetCusotmerPrivilege", YLURL];
            
            [[WHNetworkTool shareInstance] sendPostWithUrl:url header:[YLAccountTool header] params:param success:^(id responseObject) {
                
                
                if ([responseObject[@"httpCode"] integerValue] == 200) {
                    YLMemberItemGroup *group = [YLMemberItemGroup yy_modelWithDictionary:responseObject[@"httpResult"]];
                    
                    weakSelf.items = group.item;
                    
                    [subscriber sendNext:nil];
                
                    
                } else {
                    [WHProgressHUD showError:responseObject[@"message"]];
                }
                [subscriber sendCompleted];
            } fail:^(NSError *error) {
                [subscriber sendCompleted];
            }];

            return nil;
        }];
    }];
}


- (NSArray *)items
{
    if (!_items){
        _items = [NSArray array];
    }
    return _items;
}

- (void)dealloc
{
    [[WHNetworkTool shareInstance] cancel];
    [WHProgressHUD dismiss];
}


@end
